package com.robot.mvvm.ui.login

import android.graphics.BitmapFactory
import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Route
import com.robot.mvvm.R
import com.robot.mvvm.databinding.AppActivityLoginBinding
import com.robot.mvvm.vm.login.LoginViewModel
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import me.goldze.mvvmhabit.BR
import com.blankj.utilcode.util.*
import com.gyf.immersionbar.ktx.immersionBar


/**
 * 验证码登陆页面
 */
@Route(path = AppConstants.Router.Main.A_LOGIN)
class LoginActivity : GLBaseActivity<AppActivityLoginBinding, LoginViewModel>() {

    override fun initContentView(savedInstanceState: Bundle?): Int = R.layout.app_activity_login

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        immersionBar {
            transparentStatusBar()
            keyboardEnable(true)
            fitsSystemWindows(false)
            statusBarDarkFont(false)
        }
        binding.clLogin.setBackgroundResource(R.mipmap.app_bg_login_left)
        viewModel.start()
        setViewData()
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.typeEvent.observe(this) {
            binding.clLogin.setBackgroundResource(
                if (it) {
                    R.mipmap.app_bg_login_left
                } else {
                    R.mipmap.app_bg_login_right
                }
            )
        }
        viewModel.uc.codeEvent.observe(this) {
            val bitmap = BitmapFactory.decodeStream(viewModel.codeImg)
            binding.ivCode.setImageBitmap(bitmap)
        }
    }

    private fun setViewData() {
//        binding.tvPrompt.text = StringUtils.getString(R.string.app_en_tips_7)
        SpanUtils.with(binding.tvPrompt)
            .append(StringUtils.getString(R.string.app_tips_12))
            .append(StringUtils.getString(R.string.app_tips_13))
//            .setForegroundColor(Color.RED)
            .setClickSpan(ColorUtils.getColor(R.color.app_02B3FF), true) {
                ToastUtils.showShort("点击了用户协议")
            }
//            .setQuoteColor(Color.RED)
            .append(StringUtils.getString(R.string.app_tips_14))
            .setClickSpan(ColorUtils.getColor(R.color.app_02B3FF), true) {
                ToastUtils.showShort("点击了隐私协议")
            }
            .create()
    }


}